package com.example.demo.util;


import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

public class ParserNcFileUtil {
    /**
     * 解析nc文件
     * @param url nc文件绝对地址
     * @param code 数据标识
     * @return
     */
    public static <T> T getDataInfo(String url,String code) {
        try {
            Long st = System.currentTimeMillis();
            NetcdfFile nc = NetcdfFile.open(url);

            // 读nc变量
            Variable varData = nc.findVariable(code);

            T vals = (T) varData.read(new int[] { 0, 0, 0 }, varData.getShape()).copyToNDJavaArray();
            nc.close();

            Long en = System.currentTimeMillis();
            System.out.println("解析nc文件用时：" + (en - st) + "ms");
            return vals;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
